
<%
	//Get form data.
	String scholarship_id = request.getParameter("scholarship_id");
	String application_id = request.getParameter("application_id");

	java.sql.Connection conn = null;
	try {
		// Get SQL information.
		String myJDBCDriver = (String) session.getAttribute("myJDBCDriver");
		String myURL = (String) session.getAttribute("myURL");
		String myUserID = (String) session.getAttribute("myUserID");
		String myPassword = (String) session.getAttribute("myPassword");

		// Connect to the database.
		Class.forName(myJDBCDriver).newInstance();
		java.util.Properties sysprops = System.getProperties();
		sysprops.put("user", myUserID);
		sysprops.put("password", myPassword);
		conn = java.sql.DriverManager.getConnection(myURL, sysprops);

		java.sql.Statement statement = conn.createStatement();
		java.sql.ResultSet rs = statement
				.executeQuery("SELECT COUNT(*), num_recipients, applicant FROM scholarships JOIN applications ON scholarships.id=scholarship_id WHERE won=1 AND scholarship_id='"
						+ scholarship_id + "'");
		if (rs.next()) {
			// There has been people that have already been awarded.
			if (rs.getInt(1) >= rs.getInt(2)) {
				// The number of people awarded is equal to or greater than the number of allowed recipients.
				session.setAttribute("errors", "* All the money has already been awarded. No more students can recieve this scholarship.");
				response.sendRedirect("award.jsp?scholarship_id=" + scholarship_id);
			} else {
				// There are still recipients left.
				session.setAttribute("errors", "* The scholarship has been awarded to the specified student.");
				statement.executeUpdate("UPDATE applications SET won='" + 1 + "' WHERE id='" + application_id + "'");
				response.sendRedirect("award.jsp?scholarship_id=" + scholarship_id);
			}
		}
	} catch (Exception e) {
		System.out.println(e);
	} finally {
		try {
			conn.close();
		} catch (Exception ee) {
		}
	}
%>